﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ATM
{
    public partial class thongtinatm : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("TB_ATM_INFO");
        private SqlDataAdapter da = new SqlDataAdapter();

        private void connect()
        {

            String cn = @"Data Source=(local);Initial Catalog=ATM_PM1;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open();

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public thongtinatm()
        {
            InitializeComponent();
        }

        

        private void thongtinatm_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            binding();
        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select  Sys_ID[ATM],Sys_WA,Sys_DA,Sys_WT,Sys_DT,Sys_MAT,Sys_AIM,Sys_FC1,Sys_FC2,Sys_FC3,Sys_FC4,Sys_FC5,Sys_FC6
                                                        from TB_ATM_INFO ";
            da.SelectCommand = command;
            da.Fill(dt);
            showATM.DataSource = dt;
        }
        private void binding()
        {
            txtmaatm.DataBindings.Clear();
            txtmaatm.DataBindings.Add("text", showATM.DataSource, "ATM");
            txtWa.DataBindings.Clear();
            txtWa.DataBindings.Add("text", showATM.DataSource, "Sys_WA");
            txtda.DataBindings.Clear();
            txtda.DataBindings.Add("text", showATM.DataSource, "Sys_DA");
            txtwt.DataBindings.Clear();
            txtwt.DataBindings.Add("text", showATM.DataSource, "Sys_WT");
            txtdt.DataBindings.Clear();
            txtdt.DataBindings.Add("text", showATM.DataSource, "Sys_DT");
            txtmat.DataBindings.Clear();
            txtmat.DataBindings.Add("text", showATM.DataSource, "Sys_MAT");
            txtaim.DataBindings.Clear();
            txtaim.DataBindings.Add("text", showATM.DataSource, "Sys_AIM");
            txtfc1.DataBindings.Clear();
            txtfc1.DataBindings.Add("text", showATM.DataSource, "Sys_FC1");
            txtfc2.DataBindings.Clear();
            txtfc2.DataBindings.Add("text", showATM.DataSource, "Sys_FC2");
            txtfc3.DataBindings.Clear();
            txtfc3.DataBindings.Add("text", showATM.DataSource, "Sys_FC3");
            txtfc4.DataBindings.Clear();
            txtfc4.DataBindings.Add("text", showATM.DataSource, "Sys_FC4");
            txtfc5.DataBindings.Clear();
            txtfc5.DataBindings.Add("text", showATM.DataSource, "Sys_FC5");
            txtfc6.DataBindings.Clear();
            txtfc6.DataBindings.Add("text", showATM.DataSource, "Sys_FC6");
        }
        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("ATM=" + Convert.ToInt32(txtmaatm.Text))[0];
            row.BeginEdit();
            row["ATM"] = txtmaatm.Text;
            row["Sys_WA"] = txtWa.Text;
            row["Sys_DA"] = txtda.Text;
            row["Sys_WT"] = txtwt.Text;
            row["Sys_DT"] = txtdt.Text;
            row["Sys_MAT"] = txtmat.Text;
            row["Sys_AIM"] = txtaim.Text;
            row["Sys_FC1"] = txtfc1.Text;
            row["Sys_FC2"] = txtfc2.Text;
            row["Sys_FC3"] = txtfc3.Text;
            row["Sys_FC4"] = txtfc4.Text;
            row["Sys_FC5"] = txtfc5.Text;
            row["Sys_FC6"] = txtfc6.Text;
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = conn;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update TB_ATM_INFO Set Sys_WA=@Sys_WA,Sys_DA=@Sys_DA,
                                                            Sys_WT=@Sys_WT,Sys_DT=@Sys_DT,
                                                            Sys_MAT=@Sys_MAT,Sys_AIM=@Sys_AIM,
                                                            Sys_FC1=@Sys_FC1,Sys_FC2=@Sys_FC2,
                                                            Sys_FC3=@Sys_FC3,Sys_FC4=@Sys_FC4,
                                                            Sys_FC5=@Sys_FC5,Sys_FC6=@Sys_FC6
                                                               where Sys_ID=@Sys_ID";
            commandUpdate.Parameters.Add("@Sys_WA", SqlDbType.Float, 20, "Sys_WA");
            commandUpdate.Parameters.Add("@Sys_DA", SqlDbType.Float, 50, "Sys_DA");
            commandUpdate.Parameters.Add("@Sys_WT", SqlDbType.Int, 30, "Sys_WT");
            commandUpdate.Parameters.Add("@Sys_DT", SqlDbType.Int, 20, "Sys_DT");
            commandUpdate.Parameters.Add("@Sys_MAT", SqlDbType.Float, 20, "Sys_MAT");
            commandUpdate.Parameters.Add("@Sys_AIM", SqlDbType.Float, 20, "Sys_AIM");
            commandUpdate.Parameters.Add("@Sys_FC1", SqlDbType.Float, 20, "Sys_FC1");
            commandUpdate.Parameters.Add("@Sys_FC2", SqlDbType.Float, 20, "Sys_FC2");
            commandUpdate.Parameters.Add("@Sys_FC3", SqlDbType.Float, 20, "Sys_FC3");
            commandUpdate.Parameters.Add("@Sys_FC4", SqlDbType.Float, 20, "Sys_FC4");
            commandUpdate.Parameters.Add("@Sys_FC5", SqlDbType.Float, 20, "Sys_FC5");
            commandUpdate.Parameters.Add("@Sys_FC6", SqlDbType.Float, 20, "Sys_FC6");
            commandUpdate.Parameters.Add("@Sys_ID", SqlDbType.Int, 20, "ATM");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);

            MessageBox.Show("Bạn đã sửa thành công !", "thông báo", MessageBoxButtons.OK);
        }
    }
}
